/**
    @author: dt
    @date: 2025-06-23
    @description: 序列节点
*/
using System.Collections.Generic;
using UnityEngine;

namespace DLib.BehaviorTree
{
    /// <summary>
    /// 序列节点
    /// 实现AND逻辑，所有子节点都成功才返回成功
    /// 按顺序执行子节点，遇到失败就停止
    /// </summary>
    public class Sequence : CompositeNode
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Sequence() : base() { }
        
        /// <summary>
        /// 带子节点的构造函数
        /// </summary>
        /// <param name="children">子节点列表</param>
        public Sequence(List<Node> children) : base(children) { }

        /// <summary>
        /// 评估序列子节点
        /// 按顺序执行子节点：
        /// - 如果子节点失败，序列失败
        /// - 如果子节点运行中，序列运行中
        /// - 如果子节点被中断，序列被中断
        /// - 如果子节点成功，继续执行下一个子节点
        /// - 如果所有子节点都成功，序列成功
        /// </summary>
        /// <returns>序列执行状态</returns>
        protected override NodeState EvaluateChildren()
        {
            // 从当前索引开始遍历所有子节点
            for (int i = currentChildIndex; i < children.Count; i++)
            {
                currentChildIndex = i;
                Node child = children[i];
                NodeState childState = child.Evaluate();

                switch (childState)
                {
                    case NodeState.Failure:
                        // 子节点失败，序列失败
                        state = NodeState.Failure;
                        return state;
                    case NodeState.Running:
                        // 子节点运行中，序列运行中
                        state = NodeState.Running;
                        return state;
                    case NodeState.Interrupted:
                        // 子节点被中断，序列被中断
                        state = NodeState.Interrupted;
                        return state;
                    case NodeState.Success:
                        // 子节点成功，继续执行下一个子节点
                        continue;
                }
            }

            // 所有子节点都成功，序列成功
            state = NodeState.Success;
            return state;
        }
    }
} 